package com.bnz.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xy
 * @date 2022/11/15
 */
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
@Data
public class BnzElasticSearchConfig {
    private String host;
    private int port;
    @Bean
    public ElasticsearchClient elasticSearchConfig(){
        RestClient restClient = RestClient.builder(new HttpHost(host,port)).build();

        ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());

        ElasticsearchClient client = new ElasticsearchClient(transport);

        return client;
    }
}
